﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using nkstdgia.common.HoSoThamDinh;
using nkstdgia.common.ThamDinhGia.BusinessLogic;
using nkstdgia.common.ThamDinhGia.BusinessObjects;
using wsRK.ConvertionHelper;
using wsRK.Web;

namespace nkstdgia.main
{
    public partial class dsbangkhaosatdaxoa : WebPage
    {
        #region Variables

        private HoSoThamDinhDataService _hstdController = null;

        #endregion Variables

        #region Properties

        public HoSoThamDinhDataService hstdController { get { return _hstdController == null ? new HoSoThamDinhDataService() : _hstdController; } }

        #endregion Properties

        #region Construction

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._getDataQuan();
                this.ddlQuan.SelectedIndex = 0;
                this._getDataPhuong();
            }
        }

        #endregion Construction

        #region Get Data

        private void _getDataQuan()
        {
            List<THAM_SO> lsThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
            lsThamSo = (from t in lsThamSo
                        where t.LOAI_THAM_SO == "LOAI_QUAN"
                        orderby t.SEQUENCE ascending
                        select t).ToList();
            this.ddlQuan.DataSource = lsThamSo;
            this.ddlQuan.DataBind();
        }

        private void _getDataPhuong()
        {
            int intMaThamSoQuan = int.Parse(this.ddlQuan.SelectedValue);
            this._getDataPhuong(intMaThamSoQuan);
        }

        private void _getDataPhuong(int intMaThamSoQuan)
        {
            List<THAM_SO> lsThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
            lsThamSo = (from t in lsThamSo
                        where t.LOAI_THAM_SO == "LOAI_PHUONG" && t.PARENT_ID == intMaThamSoQuan
                        orderby t.SEQUENCE ascending
                        select t).ToList();
            this.ddlPhuong.DataSource = lsThamSo;
            this.ddlPhuong.DataBind();
        }

        private void _getData(int pageSize, int pageNo)
        {
            string SO_PHIEU = this.txtPhieuSo.Text;
            string TRANGTHAI = "N";
            string TEN_CHU_SU_DUNG = this.txtChuSuDung.Text;
            int QUANID = ConvertHelper.toInt(this.ddlQuan.SelectedValue);
            if (this.ddlQuan.SelectedValue == "-999999")
            {
                QUANID = -1;
            }
            int PHUONGID = ConvertHelper.toInt(this.ddlPhuong.SelectedValue);
            if (this.ddlPhuong.SelectedValue == "-999999")
            {
                PHUONGID = -1;
            }
            string DUONG = this.txtDuong.Text;
            string DOANDUONG = this.txtDoanDuong.Text;

            List<HoSoThamDinhInfo> lshs = hstdController.searchHoSoThamDinh(
                SO_PHIEU,
                TRANGTHAI,
                TEN_CHU_SU_DUNG,
                QUANID,
                PHUONGID,
                DUONG,
                DOANDUONG,
                float.MinValue,
                float.MaxValue,
                pageSize,
                pageNo);

            if (lshs != null)
            {
                if (lshs.Count > 0)
                {
                    int totalRecord = ConvertHelper.toInt(lshs[0].RowTotal);
                    this.lvDSKhaoSat.PageIndex = pageNo;
                    this.lvDSKhaoSat.PageSize = pageSize;
                    this.lvDSKhaoSat.TotalRecord = totalRecord;

                    this.lvDSKhaoSat.DataSource = lshs;
                    this.lvDSKhaoSat.DataBind();
                }
                else
                {
                    this.lvDSKhaoSat.DataSource = null;
                    this.lvDSKhaoSat.DataBind();
                }
            }
        }

        #endregion Get Data

        #region Event

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this._getData(this.lvDSKhaoSat.PageSize, 1);
            base.hideLoader();
        }

        protected void ddlQuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._getDataPhuong();
        }

        protected void lvDSKhaoSat_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "restore")
            {
                int MA_HO_SO = int.Parse(e.CommandArgument.ToString());
                if (hstdController.restoreHoSoThamDinh(MA_HO_SO) > 0)
                {
                    this._getData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex);
                    base.showMasterMessage("Đã phục hồi thành công");
                }
                else
                {
                    base.showMasterMessage("Phục hồi không thành công");
                }
                base.hideLoader();
            }
        }

        #endregion Event

        #region paging

        protected void _lv_btnFirst_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_btnLast_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = this.lvDSKhaoSat.PageTotal;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_btnNext_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = (this.lvDSKhaoSat.PageIndex == this.lvDSKhaoSat.PageTotal) ? this.lvDSKhaoSat.PageIndex : this.lvDSKhaoSat.PageIndex + 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_btnPrev_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = (this.lvDSKhaoSat.PageIndex == 1) ? 1 : this.lvDSKhaoSat.PageIndex - 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageSize = Convert.ToInt32((sender as DropDownList).SelectedValue);
            int pageNo = 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_ddlPageIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = Convert.ToInt32((sender as DropDownList).SelectedValue);
            this._getData(pageSize, pageNo);
        }

        #endregion paging
    }
}